<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>保存记录</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="../../css/public.css" media="all" />
</head>
<body class="childrenBody">
	<!--修改弹出层开始-->
	<form action="" method="get" class="layui-form" id="dataFrm2" lay-filter="dataFrm2">
		<div class="layui-form-item">
			<label class="layui-form-label">学校</label>
			<div class="layui-input-block">
				<input type="hidden" id="userId" name="userId">
				<select name="schoolId2"  id="schoolId2">
					<option value=""></option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">手机号码</label>
			<div class="layui-input-block">
				<input type="text" name="userTel2" autocomplete="off" class="layui-input" id="userTel2" lay-verify="required|phone">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">用户名</label>
			<div class="layui-input-block">
				<input type="text" name="userName2" autocomplete="off" class="layui-input" id="userName2" lay-verify="required|username">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">管理员姓名</label>
			<div class="layui-input-block">
				<input type="text" name="userRealname2" autocomplete="off" class="layui-input" id="userRealname2" lay-verify="lenLimit20">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">登录密码</label>
			<div class="layui-input-block">
				<input type="password" name="userPwd2"  id="userPwd2" class="layui-input" lay-verify="userPwd">	
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">角色</label>
			<div  class="layui-input-block layui-form">
				<select name="roleId2" id="roleId2">
					<option value="0"></option>
					<option value="1">教师</option>
					<option value="2">管理员</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">授权码</label>
			<div class="layui-input-block">
				<input type="text" name="authCode" autocomplete="off" class="layui-input" id="authCode"  lay-verify="lenLimit20">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">用户状态</label>
			<div class="layui-input-block">
				<input type="text" name="userState" autocomplete="off" class="layui-input" id="userState"  lay-verify="lenLimit20">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">职务</label> 
			<div  class="layui-input-block" >
				<input type="text" name="post" autocomplete="off" class="layui-input" id="post"  lay-verify="lenLimit20">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">备注</label> 
			<div  class="layui-input-block" >
				<input type="text" name="title" autocomplete="off" class="layui-input" id="userRemark2"  lay-verify="lenLimit50">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block" style="text-align: center;">
				<button type="button" class="layui-btn" lay-submit id="doSubmitSave" lay-filter="doSubmitSave"><i class="layui-icon">&#xe605;</i>保存</button>
				<button type="reset" class="layui-btn layui-btn-warm" ><i class="layui-icon">&#xe669;</i>重置</button>
			</div>
		</div>
	</form>
<!--修改弹出层结束-->	
<script type="text/javascript" src="../../layui/layui.js"></script>
<script >
layui.use(['form','layer','laydate','table'],function(){
    var $ = layui.jquery;
    var form = layui.form;
    var table= layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate;

	
	//console.log(parent.curRowDataJson); //yw debug
	var data = eval('('+parent.curRowDataJson+')'); //yw 解析从父页中得到当前记录的全局变量json格式
	//alert(data.userid); //yw debug
	//yw 从后台获取学校数据，填充前端的学校下拉框
	$.ajax({ //yw 从后台获取学校数据，填充前端的学校下拉框
		url:'/school/getAllSchoolName',
		async:false, //yw 取消异步下载模式，否则shoolId2第一次打开没有数据，第二次打开才有数据
		dataType:'json',
		type:'post',
		success:function(data2){
			//console.log(data); //debug
			$.each(data2,function(index,item){
				//console.log(item); //debug
				//console.log(item.schoolname); //debug
				//console.log(item.schoolid); //deubg
				$("#schoolId2").append(new Option(item.schoolname,item.schoolid));
			});
			form.render("select");                        
		}
	})
	$("#userId").val(data.userid);	
	$("#schoolId2").val(data.schoolid);
	form.render("select");
	$("#userTel2").val(data.usertel);
	$("#userName2").val(data.username);
	$("#userRealname2").val(data.userrealname);
	$("#userPwd2").val(data.userpwd);
	$("#roleId2").val(data.roleid2);
	//$("#roleId2").append(new Option('其他',3)); //debug
	form.render("select");//yw 给下拉框赋值要重新渲染一下下拉框或form.render();
	$("#authCode").val(data.authcode);
	$("#userState").val(data.userstate);
	$("#post").val(data.post);
	$("#userRemark2").val(data.userremark);              

    //提交监听事件--修改记录
    //$("#doSubmitSave").click(function(data){  //yw 使用这个方法不能进行form.verfy的验证
	form.on("submit(doSubmitSave)",function(){
        $.ajax({
            type:"POST",
            url:"/user/saveUser", 
            data:{userid:$("#userId").val(),schoolid:$("#schoolId2").val(),schoolname:$("#schoolId2").find("option:selected").text(),usertel:$("#userTel2").val(),userrealname:$("#userRealname2").val(),username:$("#userName2").val(),userpwd:$("#userPwd2").val(),roleid2:$("#roleId2").val(),autocode:$("#autoCode").val(),userstate:$("#userState").val(),post:$("#post").val(),userremark:$("#userRemark2").val()},
            success:function(res){  //yw res是后端的返回值,有的是int，有的是object;data是前端数据，data与res不能混淆
                if(res){
                    //刷新父页面
            		parent.location.reload();
                    layer.msg('保存成功',{icon:7,time:800,shade:[0.6,'#000',true]});                   
                }else{
                    layer.msg('保存失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                }
                layer.closeAll('iframe'); //yw 关闭新增界面              
            }
        });
	});
	//表单验证
	form.verify({
		required: [/[\S]+/, "不能为空"],
		username:[/^[a-zA-Z0-9_-]{4,16}$/,"用户名4-16位字母数字下划线减号"],
        phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
        email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
        url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
        integer: [/^\d*$/, "只能填写整数"],
        date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
        identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"],
        // 校验两次密码是否一致
        userPwd: [/(.+){6,12}$/, '密码必须6到12位'],
        userPwdCheck:function(value){
            if($('input[name=userPwd]').val() !== value)
                return '两次密码输入不一致！';
        },
        number: function (e) {
            if (e && isNaN(e))return "只能填写数字"
        },
        lenLimit20:function(value){
            if(value.length>20) return '长度不能超过20个字符';
        },
        lenLimit50:function(value){
            if(value.length>20) return '长度不能超过50个字符';
        }
    });


})
</script>
</body>
</html>